package cn.ibizlab.odoo.core.odoo_rating.service.impl;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.math.BigInteger;
import java.util.Collection;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import cn.ibizlab.odoo.core.client.model.Irating_rating;
import cn.ibizlab.odoo.core.odoo_rating.domain.Rating_rating;
import cn.ibizlab.odoo.core.odoo_rating.filter.Rating_ratingSearchContext;
import cn.ibizlab.odoo.core.odoo_rating.service.IRating_ratingService;
import cn.ibizlab.odoo.util.SearchResult;
import cn.ibizlab.odoo.util.log.IBIZLog;

/**
 * 实体[评级] 服务对象接口实现
 */
@Slf4j
@IBIZLog
@Service
public class Rating_ratingServiceImpl implements IRating_ratingService {

    @Autowired
    private cn.ibizlab.odoo.core.client.service.Irating_ratingClientService rating_ratingClientService;

    @Override
    public boolean update(Rating_rating rating_rating) {
        boolean bOk = false ;
        Irating_rating clientModel = this.convert2Model(rating_rating,null);
        rating_ratingClientService.update(clientModel) ;
        this.convert2Domain(clientModel,rating_rating) ;
        bOk = true;
        return bOk ;
    }

    @Override
    public void removeBatch(Collection<? extends Serializable> idList, int batchSize) {

        //rating_ratingClientService.removeBatch(rating_ratings) ;

    }

    @Override
    public void updateBatch(List<Rating_rating> rating_ratings, int batchSize) {

        //rating_ratingClientService.updateBatch(rating_ratings,batchSize) ;

    }

    @Override
    public boolean create(Rating_rating rating_rating) {
        boolean bOk = false ;
        Irating_rating clientModel = this.convert2Model(rating_rating,null);
        rating_ratingClientService.create(clientModel) ;
        this.convert2Domain(clientModel,rating_rating);
        bOk = true ;

        return bOk ;
    }

    @Override
    public void createBatch(List<Rating_rating> rating_ratings, int batchSize) {

                        //rating_ratingClientService.createBatch(rating_ratings,batchSize) ;

    }

    @Override
    public Rating_rating get(Integer id) {
        Irating_rating clientModel = rating_ratingClientService.createModel();
        clientModel.setId(id);
        rating_ratingClientService.get(clientModel) ;
        return this.convert2Domain(clientModel,null);
    }

    @Override
    public Rating_rating get(Rating_rating rating_rating) {
        Rating_rating retrating_rating = this.get(rating_rating.getId()) ;
    	BeanCopier copier=BeanCopier.create(retrating_rating.getClass(), rating_rating.getClass(), false);
        copier.copy(retrating_rating, rating_rating, null);
        return rating_rating ;
    }

    @Override
    public boolean remove(Integer id) {
        boolean bOk = false ;
        Irating_rating clientModel = rating_ratingClientService.createModel();
        clientModel.setId(id);
        rating_ratingClientService.remove(clientModel) ;
        bOk = true ;
        return bOk ;
    }

    /**
     * 查询集合 默认查询
     */
    @Override
    public Page<Rating_rating> searchDefault(Rating_ratingSearchContext context) {
        List<Rating_rating> list = new ArrayList<Rating_rating>();
        Page<cn.ibizlab.odoo.core.client.model.Irating_rating> clientModelList = rating_ratingClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Irating_rating clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Rating_rating>(list, context.getPageable(), clientModelList.getTotalElements());
    }


    /**
     * 根据消息查询
     */
    @Override
    public Page<Rating_rating> selectByOdoo_message(Integer id){
        Rating_ratingSearchContext context = new Rating_ratingSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Rating_rating> list = new ArrayList<Rating_rating>();
        Page<cn.ibizlab.odoo.core.client.model.Irating_rating> clientModelList = rating_ratingClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Irating_rating clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Rating_rating>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据联系人查询
     */
    @Override
    public Page<Rating_rating> selectByOdoo_partner(Integer id){
        Rating_ratingSearchContext context = new Rating_ratingSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Rating_rating> list = new ArrayList<Rating_rating>();
        Page<cn.ibizlab.odoo.core.client.model.Irating_rating> clientModelList = rating_ratingClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Irating_rating clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Rating_rating>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据联系人查询
     */
    @Override
    public Page<Rating_rating> selectByOdoo_rated_partner(Integer id){
        Rating_ratingSearchContext context = new Rating_ratingSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Rating_rating> list = new ArrayList<Rating_rating>();
        Page<cn.ibizlab.odoo.core.client.model.Irating_rating> clientModelList = rating_ratingClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Irating_rating clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Rating_rating>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Rating_rating> selectByOdoo_create(Integer id){
        Rating_ratingSearchContext context = new Rating_ratingSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Rating_rating> list = new ArrayList<Rating_rating>();
        Page<cn.ibizlab.odoo.core.client.model.Irating_rating> clientModelList = rating_ratingClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Irating_rating clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Rating_rating>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Rating_rating> selectByOdoo_write(Integer id){
        Rating_ratingSearchContext context = new Rating_ratingSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Rating_rating> list = new ArrayList<Rating_rating>();
        Page<cn.ibizlab.odoo.core.client.model.Irating_rating> clientModelList = rating_ratingClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Irating_rating clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Rating_rating>(list, context.getPageable(), clientModelList.getTotalElements());

    }



    public Irating_rating convert2Model(Rating_rating domain , Irating_rating model) {
        if(domain == null)
            return model ;
        if(model==null){
            model = rating_ratingClientService.createModel() ;
        }

         if(domain.getFeedbackDirtyFlag())
            model.setFeedback(domain.getFeedback());
         if(domain.getDisplay_nameDirtyFlag())
            model.setDisplay_name(domain.getDisplay_name());
         if(domain.getRatingDirtyFlag())
            model.setRating(domain.getRating());
         if(domain.getRating_textDirtyFlag())
            model.setRating_text(domain.getRating_text());
         if(domain.getParent_res_nameDirtyFlag())
            model.setParent_res_name(domain.getParent_res_name());
         if(domain.getParent_res_idDirtyFlag())
            model.setParent_res_id(domain.getParent_res_id());
         if(domain.getCreate_dateDirtyFlag())
            model.setCreate_date(domain.getCreate_date());
         if(domain.getRes_nameDirtyFlag())
            model.setRes_name(domain.getRes_name());
         if(domain.get__last_updateDirtyFlag())
            model.set__last_update(domain.get__last_update());
         if(domain.getParent_res_model_idDirtyFlag())
            model.setParent_res_model_id(domain.getParent_res_model_id());
         if(domain.getRes_idDirtyFlag())
            model.setRes_id(domain.getRes_id());
         if(domain.getConsumedDirtyFlag())
            model.setConsumed(domain.getConsumed());
         if(domain.getRating_imageDirtyFlag())
            model.setRating_image(domain.getRating_image());
         if(domain.getWrite_dateDirtyFlag())
            model.setWrite_date(domain.getWrite_date());
         if(domain.getRes_model_idDirtyFlag())
            model.setRes_model_id(domain.getRes_model_id());
         if(domain.getRes_modelDirtyFlag())
            model.setRes_model(domain.getRes_model());
         if(domain.getAccess_tokenDirtyFlag())
            model.setAccess_token(domain.getAccess_token());
         if(domain.getParent_res_modelDirtyFlag())
            model.setParent_res_model(domain.getParent_res_model());
         if(domain.getIdDirtyFlag())
            model.setId(domain.getId());
         if(domain.getPartner_id_textDirtyFlag())
            model.setPartner_id_text(domain.getPartner_id_text());
         if(domain.getRated_partner_id_textDirtyFlag())
            model.setRated_partner_id_text(domain.getRated_partner_id_text());
         if(domain.getCreate_uid_textDirtyFlag())
            model.setCreate_uid_text(domain.getCreate_uid_text());
         if(domain.getWebsite_publishedDirtyFlag())
            model.setWebsite_published(domain.getWebsite_published());
         if(domain.getWrite_uid_textDirtyFlag())
            model.setWrite_uid_text(domain.getWrite_uid_text());
         if(domain.getMessage_idDirtyFlag())
            model.setMessage_id(domain.getMessage_id());
         if(domain.getPartner_idDirtyFlag())
            model.setPartner_id(domain.getPartner_id());
         if(domain.getRated_partner_idDirtyFlag())
            model.setRated_partner_id(domain.getRated_partner_id());
         if(domain.getCreate_uidDirtyFlag())
            model.setCreate_uid(domain.getCreate_uid());
         if(domain.getWrite_uidDirtyFlag())
            model.setWrite_uid(domain.getWrite_uid());
        return model ;
    }

    public Rating_rating convert2Domain( Irating_rating model ,Rating_rating domain) {
        if(model == null)
            return domain ;
        if(domain == null){
            domain = new Rating_rating();
        }

        if(model.getFeedbackDirtyFlag())
            domain.setFeedback(model.getFeedback());
        if(model.getDisplay_nameDirtyFlag())
            domain.setDisplay_name(model.getDisplay_name());
        if(model.getRatingDirtyFlag())
            domain.setRating(model.getRating());
        if(model.getRating_textDirtyFlag())
            domain.setRating_text(model.getRating_text());
        if(model.getParent_res_nameDirtyFlag())
            domain.setParent_res_name(model.getParent_res_name());
        if(model.getParent_res_idDirtyFlag())
            domain.setParent_res_id(model.getParent_res_id());
        if(model.getCreate_dateDirtyFlag())
            domain.setCreate_date(model.getCreate_date());
        if(model.getRes_nameDirtyFlag())
            domain.setRes_name(model.getRes_name());
        if(model.get__last_updateDirtyFlag())
            domain.set__last_update(model.get__last_update());
        if(model.getParent_res_model_idDirtyFlag())
            domain.setParent_res_model_id(model.getParent_res_model_id());
        if(model.getRes_idDirtyFlag())
            domain.setRes_id(model.getRes_id());
        if(model.getConsumedDirtyFlag())
            domain.setConsumed(model.getConsumed());
        if(model.getRating_imageDirtyFlag())
            domain.setRating_image(model.getRating_image());
        if(model.getWrite_dateDirtyFlag())
            domain.setWrite_date(model.getWrite_date());
        if(model.getRes_model_idDirtyFlag())
            domain.setRes_model_id(model.getRes_model_id());
        if(model.getRes_modelDirtyFlag())
            domain.setRes_model(model.getRes_model());
        if(model.getAccess_tokenDirtyFlag())
            domain.setAccess_token(model.getAccess_token());
        if(model.getParent_res_modelDirtyFlag())
            domain.setParent_res_model(model.getParent_res_model());
        if(model.getIdDirtyFlag())
            domain.setId(model.getId());
        if(model.getPartner_id_textDirtyFlag())
            domain.setPartner_id_text(model.getPartner_id_text());
        if(model.getRated_partner_id_textDirtyFlag())
            domain.setRated_partner_id_text(model.getRated_partner_id_text());
        if(model.getCreate_uid_textDirtyFlag())
            domain.setCreate_uid_text(model.getCreate_uid_text());
        if(model.getWebsite_publishedDirtyFlag())
            domain.setWebsite_published(model.getWebsite_published());
        if(model.getWrite_uid_textDirtyFlag())
            domain.setWrite_uid_text(model.getWrite_uid_text());
        if(model.getMessage_idDirtyFlag())
            domain.setMessage_id(model.getMessage_id());
        if(model.getPartner_idDirtyFlag())
            domain.setPartner_id(model.getPartner_id());
        if(model.getRated_partner_idDirtyFlag())
            domain.setRated_partner_id(model.getRated_partner_id());
        if(model.getCreate_uidDirtyFlag())
            domain.setCreate_uid(model.getCreate_uid());
        if(model.getWrite_uidDirtyFlag())
            domain.setWrite_uid(model.getWrite_uid());
        return domain ;
    }


}
